using System;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

[Serializable]
public class StockAllData
{
    public StockData stockData;
    public List<TransitionData> stockTransitions;
    //public List<ZhuLiZiJinData>  stockZhuLiZiJin;
    public List<ZiJinLiuRuData>  stockZiJinLiuRu;
    public List<JieDuanZhuLiData>  stockJieDuanZhuLi;
    public List<ChengJiaoFenBuData>  stockChengJiaoFenBu;
    
    public Dictionary<string,TransitionData> stockTransitionsDict;
    public Dictionary<string,ZiJinLiuRuData>  stockZiJinLiuRuDict;
    public Dictionary<string,JieDuanZhuLiData>  stockJieDuanZhuLiDict;
    public Dictionary<string,ChengJiaoFenBuData>  stockChengJiaoFenBuDict;
    
    public void Init()
    {
        if (stockTransitions != null)
        {
            stockTransitionsDict = new Dictionary<string, TransitionData>();
            foreach (var stockTransition in stockTransitions)
            {
                stockTransitionsDict[GetDateKey(stockTransition.d)] = stockTransition;
            }
        }

        stockZiJinLiuRuDict = new Dictionary<string, ZiJinLiuRuData>();
        if(stockZiJinLiuRu != null)
        {
            foreach (var data in stockZiJinLiuRu)
            {
                stockZiJinLiuRuDict[GetDateKey(data.t)] = data;
            }
        }

        if (stockJieDuanZhuLi != null)
        {
            stockJieDuanZhuLiDict = new Dictionary<string, JieDuanZhuLiData>();
            foreach (var data in stockJieDuanZhuLi)
            {
                stockJieDuanZhuLiDict[GetDateKey(data.t)] = data;
            }
        }

        if (stockChengJiaoFenBu != null)
        {
            stockChengJiaoFenBuDict = new Dictionary<string, ChengJiaoFenBuData>();
            foreach (var data in stockChengJiaoFenBu)
            {
                stockChengJiaoFenBuDict[GetDateKey(data.t)] = data;
            }
        }
    }

    // public ZhuLiZiJinData GetZhuLiZiJinWithDay(DateTime checkDate)
    // {
    //     return stockZhuLiZiJin.Find(e => CompareDateDay(checkDate, e.t));
    // }
    
    public static string GetDateKey(string dateStr)
    {
        string key =  dateStr.Substring(0,10);
        //Debug.Log($"dateStr:{dateStr} to key:{key}");
        return key;
    }
    public static string GetDateKey(DateTime date)
    {
        return date.ToString("yyyy-mm-dd").Substring(0,10);
    }
    public TransitionData GetTransitionDataWithDay(DateTime checkDate)
    {
        string dateKey = GetDateKey(checkDate);
        if (stockTransitionsDict.ContainsKey(dateKey))
            return stockTransitionsDict[dateKey];
        else
            return stockTransitions.Find(e => CompareDateDay(checkDate, e.d));
    } 
    public ZiJinLiuRuData GetZiJinLiuRuWithDay(DateTime checkDate)
    {
        string dateKey = GetDateKey(checkDate);
        if (stockZiJinLiuRuDict.ContainsKey(dateKey))
            return stockZiJinLiuRuDict[dateKey];
        else
            return stockZiJinLiuRu.Find(e => CompareDateDay(checkDate, e.t));
    } 
    public JieDuanZhuLiData GetJieDuanZhuLiWithDay(DateTime checkDate)
    { 
        string dateKey = GetDateKey(checkDate);
        if (stockJieDuanZhuLiDict.ContainsKey(dateKey))
            return stockJieDuanZhuLiDict[dateKey];
        else
            return stockJieDuanZhuLi.Find(e => CompareDateDay(checkDate, e.t));
    }  
    public ChengJiaoFenBuData GetChengJiaoFenBuWithDay(DateTime checkDate)
    { 
        string dateKey = GetDateKey(checkDate);
        if (stockChengJiaoFenBuDict.ContainsKey(dateKey))
            return stockChengJiaoFenBuDict[dateKey];
        else
            return stockChengJiaoFenBu.Find(e => CompareDateDay(checkDate, e.t));
    }
    public bool CompareDateDay(DateTime date, string dateStr)
    {
        GetDateStrDay(dateStr, out int year, out int month, out int day);
        //Debug.Log($"date:{date}, dateStr year:{year}  month:{month} day:{day}");
        return date.Year == year && date.Month == month && date.Day ==day;
    }

    public static void GetDateStrDay(string dateStr, out int year, out int month, out int day)
    {
        year = month = day = 0;
        string[] strs = dateStr.Split('-');
        if (strs.Length < 3)
        {
            Debug.LogError($"parse dateStr error:{dateStr}");
            return;
        }

        int.TryParse(strs[0], out year);
        int.TryParse(strs[1], out month);
        int.TryParse(strs[2], out day);
    }
    
}